import Vue from 'vue'
import {CLEAR_CART,RECEIVE_ADDRESS,RECEIVE_CATEGORYS,RECEIVE_SHOPS,RECEIVE_USER_INFO,RESET_USER_INFO,RECEIVE_GOODS,RECEIVE_RATINGS,RECEIVE_INFO,INCREMENT_FOOD_COUNT,DECREMENT_FOOD_COUNT} from './mutation-types'
export default {
     // 根据经纬度获取地址
    [RECEIVE_ADDRESS](state,data){
       console.log(data);
       state.address = data
    },
    //  获取商品分类
    [RECEIVE_CATEGORYS](state,data){
        console.log(data);
        state.categorys=data
    },
    //  根据地址获取店铺信息
    [RECEIVE_SHOPS](state,data){
        console.log(data);
        state.shops = data
    },
     //  登录获取个人信息 //  存储个人信息
    [RECEIVE_USER_INFO](state,userInfo) {
        console.log(userInfo);
        state.userInfo = userInfo
        
    },
     //  退出登录
    [RESET_USER_INFO](state){
        state.userInfo = {}
    },
    // 商家商品goods数组获取
    [RECEIVE_GOODS](state,goods) {
        state.goods = goods
        // console.log(state.goods);
    },
    // 商家个人信息获取
    [RECEIVE_INFO](state,info) {
        state.info = info
    },
    [RECEIVE_RATINGS](state,ratings) {
        state.ratings = ratings
    },
    [INCREMENT_FOOD_COUNT](state,food){
        if (food.count) {
            food.count++
        }else {
            // food.count=1
            Vue.set(food,'count',1) //可以实现双向数据绑定
            // 把数据添加到购物车里面去
            state.cartFoods.push(food)
        }
        console.log(food);
        
    },
    [DECREMENT_FOOD_COUNT](state,food){
        console.log(food);
        if (food.count) {
            food.count--
            // --的时候如果count==0那么应该从购物车里面删除这个商品信息
            if (food.count==0) {
                state.cartFoods.splice(state.cartFoods.indexOf(food),1)
            }
        }
    },
    [CLEAR_CART](state){
        // 清楚food中的count
        state.cartFoods.forEach(v =>v.count=0);
        // 移除购物车中的所有信息
        state.cartFoods = []
    }
}